package com.ys.controller.web.business;

import com.ys.config.MenuId;
import com.ys.config.MyConfig;
import com.ys.controller.api.business.ApiInterTpProductBqController;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.business.TpProductBq;
import com.ys.entity.business.TpProductBqSp;
import com.ys.entity.sys.YsProcInst;
import com.ys.enume.flow.YsProcInstEnum;
import com.ys.service.business.TpProductBqService;
import com.ys.service.sys.CommonApiService;
import com.ys.util.DateUtil;
import com.ys.vo.W;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/web/business/tpProductBq")
public class WebTpProductBqController {
	

	@Autowired
	private CommonApiService<TpProductBq> commonApiService;
	@Autowired
	private TpProductBqService tpProductBqService;
	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
		try {
			String commodityProperty = params.get("commodityProperty").toString();
			String bid = commonApiService.getCommonNextBid(ApiInterTpProductBqController.MENU_ID);

			if (!"test".equals(MyConfig.getActive()) && !"pro".equals(MyConfig.getActive())) {
				// 子列表配置信息
				SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TP_ProductBq, new ArrayList<Map<String, Object>>());

				Map<String, Object> extraParam = new HashMap<>();
				extraParam.put("commodityProperty",commodityProperty);
				List<SubGridConfigDto.ButtonDto> buttonArr =  subGridConfig.getButtonArr();
				buttonArr.get(0).setName(buttonArr.get(0).getName()+commodityProperty);
				subGridConfig.setButtonArr(buttonArr);
				if (commodityProperty.equals("优品")){
					for (SubGridConfigDto.ColGridDto colGridDto :subGridConfig.getColGridArr()){
						if (colGridDto.getField() != null && colGridDto.getField().equals("name")){
							colGridDto.setStateEdit(0);
						}
					}
				}else if (commodityProperty.equals("精品")){
					for (SubGridConfigDto.ColGridDto colGridDto :subGridConfig.getColGridArr()){
						if (colGridDto.getField() != null && colGridDto.getField().equals("code")){
							colGridDto.setStateEdit(1);
						}
					}
				}
				return commonApiService.toAdd(model, new TpProductBq().setBid(bid), ApiInterTpProductBqController.MENU_ID, extraParam, subGridConfig);
			} else {
				commonApiService.toAdd(model, new TpProductBq().setBid(bid), ApiInterTpProductBqController.MENU_ID, null, null);
				model.addAttribute("commodityProperty", commodityProperty);
				return "business/tpProductBq/tpProductBqAdd";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}



	/*
	 * 编辑
	 */
//	@RequiresPermissions("tpProductBq:edit")
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
		try {
			if (params.get("bid") == null){
				return W.error(model, "单号不能为空");
			}


			Map entity = tpProductBqService.getExtraParam(params.get("bid").toString());
			if (entity == null){
				return W.error(model, "该商品备案申请已删除!");
			}

			List<Map<String, Object>> tpProductBqSqList = new TpProductBqSp()
					.setPid(Long.valueOf(entity.get("id").toString()))
					.queryMap();

			YsProcInst yspProcInst = new YsProcInst().setBid(String.valueOf(entity.get("bid"))).queryFirst();
			Integer onProcess = yspProcInst.getStateFlow() == YsProcInstEnum.StateFlow.INIT.value ? 0 : 1;


			String commodityProperty = null;
			if (!"test".equals(MyConfig.getActive()) && !"pro".equals(MyConfig.getActive())) {
				// 子列表配置信息
				SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TP_ProductBq, tpProductBqSqList);

				commodityProperty = String.valueOf(tpProductBqSqList.get(0).get("commodityProperty"));
				entity.put("commodityProperty",commodityProperty);

				List<SubGridConfigDto.ButtonDto> buttonArr =  subGridConfig.getButtonArr();
				buttonArr.get(0).setName(buttonArr.get(0).getName()+commodityProperty);
				subGridConfig.setButtonArr(buttonArr);
				if (commodityProperty.equals("优品")){
					for (SubGridConfigDto.ColGridDto colGridDto :subGridConfig.getColGridArr()){
						if (colGridDto.getField() != null && colGridDto.getField().equals("name")){
							colGridDto.setStateEdit(0);
						}
					}
				}else if (commodityProperty.equals("精品")){
					for (SubGridConfigDto.ColGridDto colGridDto :subGridConfig.getColGridArr()){
						if (colGridDto.getField() != null && colGridDto.getField().equals("code")){
							colGridDto.setStateEdit(1);
						}
					}
				}
				return commonApiService.toEdit(model, Long.valueOf(entity.get("id").toString()), new TpProductBq(), ApiInterTpProductBqController.MENU_ID, entity, subGridConfig);
			} else {
				commonApiService.toEdit(model, Long.valueOf(entity.get("id").toString()), new TpProductBq(), ApiInterTpProductBqController.MENU_ID, entity, null);
				model.addAttribute("onProcess", onProcess);
				model.addAttribute("sonlist", tpProductBqSqList);
				model.addAttribute("commodityProperty", tpProductBqSqList.get(0).get("commodityProperty"));
				return "business/tpProductBq/tpProductBqEdit";
			}
		}catch (Exception e){
			return e.getMessage();
		}
	}

}
